home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / libguile / scmsigs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-14  |  9.6 KB  |  497 lines

  1. /*    Copyright (C) 1995 Free Software Foundation, Inc.
  2.  * 
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2, or (at your option)
  6.  * any later version.
  7.  * 
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this software; see the file COPYING.  If not, write to
  15.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  *
  17.  * As a special exception, the Free Software Foundation gives permission
  18.  * for additional uses of the text contained in its release of GUILE.
  19.  *
  20.  * The exception is that, if you link the GUILE library with other files
  21.  * to produce an executable, this does not by itself cause the
  22.  * resulting executable to be covered by the GNU General Public License.
  23.  * Your use of that executable is in no way restricted on account of
  24.  * linking the GUILE library code into it.
  25.  *
  26.  * This exception does not however invalidate any other reasons why
  27.  * the executable file might be covered by the GNU General Public License.
  28.  *
  29.  * This exception applies only to the code released by the
  30.  * Free Software Foundation under the name GUILE.  If you copy
  31.  * code from other Free Software Foundation releases into a copy of
  32.  * GUILE, as the General Public License permits, the exception does
  33.  * not apply to the code that you add in this way.  To avoid misleading
  34.  * anyone as to the status of such modified files, you must delete
  35.  * this exception notice from them.
  36.  *
  37.  * If you write modifications of your own for GUILE, it is your choice
  38.  * whether to permit this exception to apply to your modifications.
  39.  * If you do not wish that, delete this exception notice.  
  40.  */
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <signal.h>
  45. #include "_scm.h"
  46.  
  47.  
  48.  
  49. #if (__TURBOC__==1)
  50. #define signal ssignal        /* Needed for TURBOC V1.0 */
  51. #endif
  52.  
  53.  
  54.  
  55. /* SIGRETTYPE is the type that signal handlers return.  See <signal.h>*/
  56.  
  57. #ifdef RETSIGTYPE
  58. #define SIGRETTYPE RETSIGTYPE
  59. #else
  60. #ifdef STDC_HEADERS
  61. #if (__TURBOC__==1)
  62. #define SIGRETTYPE int
  63. #else
  64. #define SIGRETTYPE void
  65. #endif
  66. #else
  67. #ifdef linux
  68. #define SIGRETTYPE void
  69. #else
  70. #define SIGRETTYPE int
  71. #endif
  72. #endif
  73. #endif
  74.  
  75. #ifdef vms
  76. #ifdef __GNUC__
  77. #define SIGRETTYPE int
  78. #endif
  79. #endif
  80.  
  81. #ifdef SIGHUP
  82. #ifdef __STDC__
  83. static SIGRETTYPE 
  84. hup_signal (int sig)
  85. #else
  86. static SIGRETTYPE 
  87. hup_signal (sig)
  88.      int sig;
  89. #endif
  90. {
  91.   signal (SIGHUP, hup_signal);
  92.   scm_wta (SCM_UNDEFINED, (char *) HUP_SIGNAL, "");
  93. }
  94. #endif
  95.  
  96.  
  97. #ifdef __STDC__
  98. static SIGRETTYPE 
  99. int_signal (int sig)
  100. #else
  101. static SIGRETTYPE 
  102. int_signal (sig)
  103.      int sig;
  104. #endif
  105. {
  106.   sig = errno;
  107.   signal (SIGINT, int_signal);
  108.   if (scm_ints_disabled)
  109.     scm_sig_deferred = 1;
  110.   else
  111.     scm_han_sig ();
  112.   errno = sig;
  113. }
  114.  
  115. /* If doesn't have SIGFPE, disable FLOATS for the rest of this file. */
  116.  
  117. #ifndef SIGFPE
  118. #undef FLOATS
  119. #endif
  120.  
  121. #ifdef FLOATS
  122. #ifdef __STDC__
  123. static SIGRETTYPE 
  124. fpe_signal (int sig)
  125. #else
  126. static SIGRETTYPE 
  127. fpe_signal (sig)
  128.      int sig;
  129. #endif
  130. {
  131.   signal (SIGFPE, fpe_signal);
  132.   scm_wta (SCM_UNDEFINED, (char *) FPE_SIGNAL, "");
  133. }
  134. #endif
  135.  
  136.  
  137. #ifdef SIGBUS
  138. #ifdef __STDC__
  139. static SIGRETTYPE 
  140. bus_signal (int sig)
  141. #else
  142. static SIGRETTYPE 
  143. bus_signal (sig)
  144.      int sig;
  145. #endif
  146. {
  147.   signal (SIGBUS, bus_signal);
  148.   scm_wta (SCM_UNDEFINED, (char *) BUS_SIGNAL, "");
  149. }
  150. #endif
  151.  
  152.  
  153. #ifdef SIGSEGV            /* AMIGA lacks! */
  154. #ifdef __STDC__
  155. static SIGRETTYPE 
  156. segv_signal (int sig)
  157. #else
  158. static SIGRETTYPE 
  159. segv_signal (sig)
  160.      int sig;
  161. #endif
  162. {
  163.   signal (SIGSEGV, segv_signal);
  164.   scm_wta (SCM_UNDEFINED, (char *) SEGV_SIGNAL, "");
  165. }
  166. #endif
  167. #ifdef atarist
  168. #undef SIGALRM            /* only available via MiNT libs */
  169. #endif
  170. #ifdef GO32
  171. #undef SIGALRM
  172. #endif
  173. #ifdef __HIGHC__
  174. # undef SIGALRM
  175. #endif
  176. #ifdef SIGALRM
  177. #ifdef __STDC__
  178. static SIGRETTYPE 
  179. alrm_signal (int sig)
  180. #else
  181. static SIGRETTYPE 
  182. alrm_signal (sig)
  183.      int sig;
  184. #endif
  185. {
  186.   sig = errno;
  187.   signal (SIGALRM, alrm_signal);
  188.   if (scm_ints_disabled)
  189.     scm_alrm_deferred = 1;
  190.   else
  191.     scm_han_alrm ();
  192.   errno = sig;
  193. }
  194.  
  195. PROC (s_alarm, "alarm", 1, 0, 0, scm_alarm);
  196. #ifdef __STDC__
  197. SCM 
  198. scm_alarm (SCM i)
  199. #else
  200. SCM 
  201. scm_alarm (i)
  202.      SCM i;
  203. #endif
  204. {
  205.   unsigned int j;
  206.   ASSERT (INUMP (i) && (INUM (i) >= 0), i, ARG1, s_alarm);
  207.   SYSCALL (j = alarm (INUM (i)));
  208.   return MAKINUM (j);
  209. }
  210.  
  211.  
  212. #ifndef AMIGA
  213. PROC (s_pause, "pause", 0, 0, 0, scm_pause);
  214. #ifdef __STDC__
  215. SCM 
  216. scm_pause (void)
  217. #else
  218. SCM 
  219. scm_pause ()
  220. #endif
  221. {
  222.   pause ();
  223.   return UNSPECIFIED;
  224. }
  225. #endif
  226. #endif /* SIGALRM */
  227.  
  228. #ifndef AMIGA
  229. # ifndef _Windows
  230.  
  231. PROC (s_sleep, "sleep", 1, 0, 0, scm_sleep);
  232. #ifdef __STDC__
  233. SCM 
  234. scm_sleep (SCM i)
  235. #else
  236. SCM 
  237. scm_sleep (i)
  238.      SCM i;
  239. #endif
  240. {
  241.   unsigned int j;
  242.   ASSERT (INUMP (i) && (INUM (i) >= 0), i, ARG1, s_sleep);
  243. #ifdef __HIGHC__
  244.   SYSCALL(j = 0; sleep(INUM(i)););
  245. #else
  246.   SYSCALL(j = sleep(INUM(i)););
  247. #endif
  248.   return MAKINUM (j);
  249. }
  250. # endif
  251. #endif
  252.  
  253.  
  254.  
  255. #ifndef GO32
  256. /* int raise P((int sig)); */
  257. PROC (s_raise, "raise", 1, 0, 0, scm_raise);
  258. #ifdef __STDC__
  259. SCM
  260. scm_raise(SCM sig)
  261. #else
  262. SCM
  263. scm_raise(sig)
  264.      SCM sig;
  265. #endif
  266. {
  267.   ASSERT(INUMP(sig), sig, ARG1, s_raise);
  268. # ifdef vms
  269.   return MAKINUM(gsignal((int)INUM(sig)));
  270. # else
  271.   return kill (getpid(), (int)INUM(sig)) ? BOOL_F : BOOL_T;
  272. # endif
  273. }
  274. #endif
  275.  
  276. #ifdef TICKS
  277. unsigned int scm_tick_count = 0, scm_ticken = 0;
  278. SCM *scm_loc_tick_signal;
  279. #ifdef __STDC__
  280. void 
  281. scm_tick_signal (void)
  282. #else
  283. void 
  284. scm_tick_signal ()
  285. #endif
  286. {
  287.   if (scm_ticken && NIMP (*scm_loc_tick_signal))
  288.     {
  289.       scm_ticken = 0;
  290.       scm_apply (*scm_loc_tick_signal, EOL, EOL);
  291.     }
  292. }
  293.  
  294.  
  295. PROC (s_ticks, "ticks", 1, 0, 0, scm_ticks);
  296. #ifdef __STDC__
  297. SCM 
  298. scm_ticks (SCM i)
  299. #else
  300. SCM 
  301. scm_ticks (i)
  302.      SCM i;
  303. #endif
  304. {
  305.   SCM j = scm_ticken ? scm_tick_count : 0;
  306.   if (!UNBNDP (i))
  307.     scm_ticken = scm_tick_count = INUM (i);
  308.   return MAKINUM (j);
  309. }
  310. #endif
  311.  
  312. #ifdef SIGHUP
  313. static SIGRETTYPE (*oldhup) ();
  314. #endif
  315. static SIGRETTYPE (*oldint) ();
  316. #ifdef FLOATS
  317. static SIGRETTYPE (*oldfpe) ();
  318. #endif
  319. #ifdef SIGBUS
  320. static SIGRETTYPE (*oldbus) ();
  321. #endif
  322. #ifdef SIGSEGV            /* AMIGA lacks! */
  323. static SIGRETTYPE (*oldsegv) ();
  324. #endif
  325. #ifdef SIGALRM
  326. static SIGRETTYPE (*oldalrm) ();
  327. #endif
  328. #ifdef SIGPIPE
  329. static SIGRETTYPE (*oldpipe) ();
  330. #endif
  331.  
  332.  
  333. #ifdef __STDC__
  334. void 
  335. scm_init_signals (void)
  336. #else
  337. void 
  338. scm_init_signals ()
  339. #endif
  340. {
  341.   oldint = signal (SIGINT, int_signal);
  342. #ifdef SIGHUP
  343.   oldhup = signal (SIGHUP, hup_signal);
  344. #endif
  345. #ifdef FLOATS
  346.   oldfpe = signal (SIGFPE, fpe_signal);
  347. #endif
  348. #ifdef SIGBUS
  349.   oldbus = signal (SIGBUS, bus_signal);
  350. #endif
  351. #ifdef SIGSEGV            /* AMIGA lacks! */
  352.   oldsegv = signal (SIGSEGV, segv_signal);
  353. #endif
  354. #ifdef SIGALRM
  355.   alarm (0);            /* kill any pending ALRM interrupts */
  356.   oldalrm = signal (SIGALRM, alrm_signal);
  357. #endif
  358. #ifdef SIGPIPE
  359.   oldpipe = signal (SIGPIPE, SIG_IGN);
  360. #endif
  361. #ifdef ultrix
  362.   siginterrupt (SIGINT, 1);
  363.   siginterrupt (SIGALRM, 1);
  364.   siginterrupt (SIGHUP, 1);
  365.   siginterrupt (SIGPIPE, 1);
  366. #endif /* ultrix */
  367. }
  368.  
  369. /* This is used in preparation for a possible fork().  Ignore all
  370.    signals before the fork so that child will catch only if it
  371.    establishes a handler */
  372. #ifdef __STDC__
  373. void 
  374. scm_ignore_signals (void)
  375. #else
  376. void 
  377. scm_ignore_signals ()
  378. #endif
  379. {
  380. #ifdef ultrix
  381.   siginterrupt (SIGINT, 0);
  382.   siginterrupt (SIGALRM, 0);
  383.   siginterrupt (SIGHUP, 0);
  384.   siginterrupt (SIGPIPE, 0);
  385. #endif /* ultrix */
  386.   signal (SIGINT, SIG_IGN);
  387. #ifdef SIGHUP
  388.   signal (SIGHUP, SIG_DFL);
  389. #endif
  390. #ifdef FLOATS
  391.   signal (SIGFPE, SIG_DFL);
  392. #endif
  393. #ifdef SIGBUS
  394.   signal (SIGBUS, SIG_DFL);
  395. #endif
  396. #ifdef SIGSEGV            /* AMIGA lacks! */
  397.   signal (SIGSEGV, SIG_DFL);
  398. #endif
  399.   /* Some documentation claims that ALRMs are cleared accross forks.
  400.      If this is not always true then the value returned by alarm(0)
  401.      will have to be saved and scm_unignore_signals() will have to
  402.      reinstate it. */
  403.   /* This code should be neccessary only if the forked process calls
  404.      alarm() without establishing a handler:
  405.      #ifdef SIGALRM
  406.      oldalrm = signal(SIGALRM, SIG_DFL);
  407.      #endif */
  408.   /* These flushes are per warning in man page on fork(). */
  409.   fflush (stdout);
  410.   fflush (stderr);
  411. }
  412.  
  413. #ifdef __STDC__
  414. void 
  415. scm_unignore_signals (void)
  416. #else
  417. void 
  418. scm_unignore_signals ()
  419. #endif
  420. {
  421.   signal (SIGINT, int_signal);
  422. #ifdef SIGHUP
  423.   signal (SIGHUP, hup_signal);
  424. #endif
  425. #ifdef FLOATS
  426.   signal (SIGFPE, fpe_signal);
  427. #endif
  428. #ifdef SIGBUS
  429.   signal (SIGBUS, bus_signal);
  430. #endif
  431. #ifdef SIGSEGV            /* AMIGA lacks! */
  432.   signal (SIGSEGV, segv_signal);
  433. #endif
  434. #ifdef SIGALRM
  435.   signal (SIGALRM, alrm_signal);
  436. #endif
  437. #ifdef ultrix
  438.   siginterrupt (SIGINT, 1);
  439.   siginterrupt (SIGALRM, 1);
  440.   siginterrupt (SIGHUP, 1);
  441.   siginterrupt (SIGPIPE, 1);
  442. #endif /* ultrix */
  443. }
  444.  
  445. #ifdef __STDC__
  446. void 
  447. scm_restore_signals (void)
  448. #else
  449. void 
  450. scm_restore_signals ()
  451. #endif
  452. {
  453. #ifdef ultrix
  454.   siginterrupt (SIGINT, 0);
  455.   siginterrupt (SIGALRM, 0);
  456.   siginterrupt (SIGHUP, 0);
  457.   siginterrupt (SIGPIPE, 0);
  458. #endif /* ultrix */
  459.   signal (SIGINT, oldint);
  460. #ifdef SIGHUP
  461.   signal (SIGHUP, oldhup);
  462. #endif
  463. #ifdef FLOATS
  464.   signal (SIGFPE, oldfpe);
  465. #endif
  466. #ifdef SIGBUS
  467.   signal (SIGBUS, oldbus);
  468. #endif
  469. #ifdef SIGSEGV            /* AMIGA lacks! */
  470.   signal (SIGSEGV, oldsegv);
  471. #endif
  472. #ifdef SIGPIPE
  473.   signal (SIGPIPE, oldpipe);
  474. #endif
  475. #ifdef SIGALRM
  476.   alarm (0);            /* kill any pending ALRM interrupts */
  477.   signal (SIGALRM, oldalrm);
  478. #endif
  479. }
  480.  
  481.  
  482. #ifdef __STDC__
  483. void
  484. scm_init_scmsigs (void)
  485. #else
  486. void
  487. scm_init_scmsigs ()
  488. #endif
  489. {
  490. #ifdef TICKS
  491.   scm_loc_tick_signal = &CDR(scm_sysintern("ticks-interrupt", SCM_UNDEFINED));
  492.   scm_make_subr(s_ticks, tc7_subr_1o, scm_ticks);
  493. #endif
  494. #include "scmsigs.x"
  495. }
  496.  
  497.